package com.cqs.leetcode;

/**
 * @author lixiaowen
 * @create 2019-12-20
 */
public class RobotReturntoOrigin657 {


    static class Solution {

        private int x = 0, y = 0;

        public boolean judgeCircle(String moves) {
            movie(moves, moves.length());
            return x == 0 && y == 0;
        }

        /**
         *
         * @param moves
         * @param step 还剩多少步
         */
        private void movie(final String moves, int step) {
            if (Math.abs(x) > step || Math.abs(y) > step || step == 0) return;
            switch (moves.charAt(--step)) {
                case 'U':
                    y++;
                    break;
                case 'D':
                    y--;
                    break;
                case 'L':
                    --x;
                    break;
                case 'R':
                    ++x;
                    break;
                default:
                    break;
            }

            movie(moves, step);
        }
    }

    static class Solution2 {
        public boolean judgeCircle(String moves) {
            if (moves == null) return false;
            int[] count = new int[4];
            for (int i = 0; i < moves.length(); ++i) {
                switch (moves.charAt(i)) {
                    case 'U':
                        count[0]++;
                        break;
                    case 'D':
                        count[1]++;
                        break;
                    case 'L':
                        count[2]++;
                        break;
                    case 'R':
                        count[3]++;
                        break;
                    default:
                        break;
                }
            }
            return (count[0] == count[1]) && (count[2] == count[3]);
        }
    }
}
